package game.block.input;

import game.block.Constants;
import game.block.tile.Tile;

import java.util.ArrayList;

public class DraggedMove implements Constants {
    
    private ArrayList<Tile> explodeTiles;
    private ArrayList<Tile> tileList;
    private ArrayList<Tile> adjacentTiles;
    private ArrayList<Tile> queuedTiles;
    private Tile currentTile;
    private int cols;
    private int rows;

    // rows and cols switched
    public DraggedMove(ArrayList<Tile> tileList, int cols, int rows) {
        this.tileList = tileList;
        explodeTiles = new ArrayList<Tile>();
        adjacentTiles=new ArrayList<Tile>();
        queuedTiles=new ArrayList<Tile>();
        this.cols = cols;
        this.rows = rows;
    }
    
    public void startDrag(Tile t){
        currentTile = t;
        queuedTiles.clear();
        queuedTiles.add(t);
        updateAdjacentTiles();
    }

    public void updateAdjacentTiles() {
        adjacentTiles.clear();
        int cPos = (int) (currentTile.getPosition().x / TILE_SIZE);
        int rPos = (int) (currentTile.getPosition().y / TILE_SIZE);
        int arrayPos = (rows - 1 - rPos) * cols + cPos;
        // add left tile
        if (arrayPos % cols != 0) {
            Tile tile = tileList.get(arrayPos - 1);
            if (!tile.hasEntity()) {
                adjacentTiles.add(tile);
            }
        }
        // add right tile
        if (arrayPos % cols != cols - 1) {
            Tile tile = tileList.get(arrayPos + 1);
            if (!tile.hasEntity()) {
                adjacentTiles.add(tile);
            }
        }
        // add top tile
        if (arrayPos - cols > 0) {
            Tile tile = tileList.get(arrayPos - cols);
            if (!tile.hasEntity()) {
                adjacentTiles.add(tile);
            }
        }
        // add bottom tile
        if (arrayPos + cols < tileList.size()) {
            Tile tile = tileList.get(arrayPos + cols);
            if (!tile.hasEntity()) {
                adjacentTiles.add(tile);
            }
        }

    }
    
    public ArrayList<Tile> getAdjacentTiles() {
        return adjacentTiles;
    }

    public void setAdjacentTiles(ArrayList<Tile> adjacentTiles) {
        this.adjacentTiles = adjacentTiles;
    }

    public ArrayList<Tile> getQueuedTiles() {
        return queuedTiles;
    }

    public void setQueuedTiles(ArrayList<Tile> queuedTiles) {
        this.queuedTiles = queuedTiles;
    }

    public Tile getCurrentTile() {
        return currentTile;
    }

    public void setCurrentTile(Tile currentTile) {
        this.currentTile = currentTile;
    }

    public ArrayList<Tile> getExplodeTiles() {
        return explodeTiles;
    }

    public void setExplodeTiles(ArrayList<Tile> explodeTiles) {
        this.explodeTiles = explodeTiles;
    }

    

}
